package com.sgb.kjwl.view.ui.fragment.home;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.BaseHeightViewPagerFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrHomeMerchantsBinding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.BannerEntity;
import com.sgb.kjwl.view.event.CategoryEvent2;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.viewmodel.home.MerchantsFragModel;
import com.sgb.link.entity.merchant.InvestmentProjectEntity;
import com.sgb.link.entity.merchant.TradeCategoryEntity;
import com.sgb.merchant.view.adapter.InvestmentProjectAdapter;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

/*
 * 招商引资
 * 2021/7/19
 * */
public class MerchantsFragment extends BaseHeightViewPagerFragment {

    @Inject
    MerchantsFragModel viewModel;
    private FrHomeMerchantsBinding binding;

    private int mItemType;
    private TradeCategoryEntity entity;
    private InvestmentProjectAdapter mAdapter;

    public static MerchantsFragment newInstance(int position, BannerEntity bannerEntity, boolean isRefresh) {
        MerchantsFragment fragment = new MerchantsFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM, bannerEntity);
        args.putInt(ARG_PARAM_POS, position);
        args.putBoolean(ARG_isRefresh, isRefresh);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() == null) {
            return;
        }
        mItemType = getItemType();
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_home_merchants, container, false);
        return binding.getRoot();
    }

    private int getItemType() {
        int mItemType = -1;
        BannerEntity bannerEntity = (BannerEntity) getArguments().getSerializable(ARG_PARAM);
        switch (bannerEntity.getName()) {
            case "投资项目":
                mItemType = 0;
                break;
        }
        return mItemType;
    }

    @Override
    protected void initView() {
        super.initView();
        if (getArguments() == null) {
            return;
        }
        mItemType = getItemType();
        if (mItemType == -1) {
            return;
        }
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.recycler.setNestedScrollingEnabled(false);
        binding.recycler.setNestedScrollingEnabled(false);
        binding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = getResources().getDimensionPixelSize(R.dimen.dp1);
            }
        });
        mAdapter = new InvestmentProjectAdapter(getActivity(), null);
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData(true);
            }
        });
        binding.tvGoModel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(CategoryEvent2.make(EventConstant.EVENT_CATEGORY_CHOOSE2, 27));
            }
        });
        binding.tvGoModel1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(CategoryEvent2.make(EventConstant.EVENT_CATEGORY_CHOOSE2, 27));
            }
        });
    }

    public void IsRefresh(boolean isRefresh) {
        setIsRefresh(isRefresh);
        loadData(false);
    }

    private void loadData(boolean isLoadMore) {
        if (entity == null){
            return;
        }
        viewModel.getInvestmentData(mItemType,entity.getNo(), isLoadMore);
    }

    @Override
    protected void onLazyLoad() {
        viewModel.getindustryFirstCategoryList(mItemType);
    }

    public void showCategoryTypeView(List<TradeCategoryEntity> data) {
        if (data == null)
            return;
        TradeCategoryEntity tradeCategoryEntity = new TradeCategoryEntity();
        tradeCategoryEntity.setNo("");
        tradeCategoryEntity.setCategroyName("全部");
        data.add(0,tradeCategoryEntity);
        binding.tabLayout.clearOnTabSelectedListeners();
        binding.tabLayout.removeAllTabs();
        for (int i = 0; i < data.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(data.get(i).getCategroyName()));
        }
        binding.tabLayout.scrollTo(0, 0);
        entity = data.get(0);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                entity = data.get(tab.getPosition());
                loadData(false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        loadData(false);
    }

    public void showList(List<InvestmentProjectEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                mAdapter.addDatas(list);
                binding.recycler.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                if (list.size() < 3) {
                    binding.lineMore.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    binding.lineMore.setVisibility(View.VISIBLE);
                }
            } else {
                binding.lineMore.setVisibility(View.VISIBLE);
                binding.tvMore.setVisibility(View.GONE);
                binding.recycler.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        } else {
            if (list != null && list.size() > 0) {
                binding.lineMore.setVisibility(View.VISIBLE);
                binding.recycler.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                if (list.size() < 3) {
                    binding.lineMore.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    binding.tvMore.setVisibility(View.VISIBLE);
                    binding.lineMore.setVisibility(View.VISIBLE);
                }
            } else {
                binding.lineMore.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recycler.setVisibility(View.GONE);
            }
            mAdapter.setDatas(list);
        }
        binding.recycler.setAdapter(mAdapter);
    }
}
